#ifndef VSSAMPLE_H
#define VSSAMPLE_H
#include "VSApplication.h"
#include "VSCamera.h"
#include "VS1stCameraController.h"
#include "VSGeometry.h"
#include "VSMaterial.h"
#include "VSTriangleSet.h"
#include "VSRectangle3.h"
#include "VSDirectionLight.h"
#include "VSOrdinaryTriangleSet.h"
#include "VSStaticMeshNode.h"
#include "VSScene.h"
#include "VSSceneManager.h"
#include "VSEngineInput.h"
#include "VSCLodTerrainNode.h"
#include "VSDLodTerrainNode.h"
#include "VSLineSet.h"
#include "VSPointLight.h"
#include "VSSkelectonMeshNode.h"
#include "VSBezierCurve3.h"
#include "VSCurve3SubdivisionByLength.h"
#include "VSCurve3SubdivisionByError.h"
#include "VSQueue.h"
#include "VSSafeQueue.h"
#include "VSBitArray.h"
#include "VSTimer.h"
#include "VSRenderThread.h"
#include "VSGraph.h"
#define  TEST_SKE_NUM 5
#include "TestSaveLoad.h"
#include "VSSkelectonActor.h"
#include "VSStaticActor.h"
#include "VSWorld.h"
#include "VSCameraActor.h"
#include "VSLightActor.h"
#include "VSTerrainActor.h"
#include "VSPostEffectFunction.h"
namespace VSEngine2
{
	
	class VSSample : public VSApplication
	{
	public:
		VSSample();
		virtual ~VSSample();
		DLCARE_APPLICATION(VSSample);
	public:
		virtual bool PreInitial();
		virtual bool OnInitial();	
		virtual bool OnTerminal();
		virtual bool PostUpdate();
		virtual bool OnDraw();
		virtual void OnMove(int xPos,int yPos);
		virtual void OnReSize(int iWidth,int iHeight);
		virtual void OnKeyDown(unsigned int uiKey);
		virtual void OnKeyUp(unsigned int uiKey);
		virtual void OnLButtonDown(int xPos,int yPos);
		virtual void OnLButtonUp(int xPos,int yPos);
		virtual void OnRButtonDown(int xPos,int yPos);
		virtual void OnRButtonUp(int xPos,int yPos);
		virtual void OnMButtonDown(int xPos,int yPos);
		virtual void OnMButtonUp(int xPos,int yPos);
		virtual void OnMouseMove(int xPos,int yPos);
		virtual void OnMouseWheel(int xPos,int yPos,int zDet);
	protected:
		VSCameraActorPtr m_pCameraActor;
		VS1stCameraControllerPtr m_p1stCameraController;
		bool	m_bLMouseDowning;
		int		m_iMouseX;
		int		m_iMouseY;
		int		m_iLastMouseX;
		int		m_iLastMouseY;
		VSRasterizerStatePtr  m_pWireFrameStateEnable;
		bool	m_bWireSwith;

		float m_fPencent;

		VSSkelectonActorPtr m_pSkelMeshActor[TEST_SKE_NUM * TEST_SKE_NUM];
		VSDirectionLightActorPtr m_pDirecttionLight;
		VSDirectionLightActorPtr m_pDirecttionLight1;

		float m_fAngle;
		VSPostEffectSetRPtr m_pPEGraySet;
		void AddASYNTexture();
		void AddTexture();
	};
	IMPLEMENT_APPLICATION(VSSample);
	VSSample::VSSample()
	{
		m_iMouseX = 0;
		m_iMouseY = 0;
		m_bLMouseDowning = false;
		m_bWireSwith = false;
		m_fPencent = 1.0f;
		m_fAngle = 0;
	}
	VSSample::~VSSample()
	{

	}
	bool VSSample::PreInitial()
	{
		if (!VSApplication::PreInitial())
		{
			return false;
		}
		for (unsigned int i = 0 ; i < TEST_SKE_NUM * TEST_SKE_NUM ;i++)
		{
			m_pSkelMeshActor[i] = NULL;
		}
		m_pCameraActor = NULL;
		m_p1stCameraController = NULL;
		m_pWireFrameStateEnable = NULL;
		m_pDirecttionLight = NULL;
		m_pDirecttionLight1 = NULL;
		m_pPEGraySet = NULL;
		return true;
	}
	bool VSSample::OnInitial()
	{
		if (!VSApplication::OnInitial())
		{
			return false;
		}
		//-------------------------------------------------create camera

		m_pCameraActor = VS_NEW VSCameraActor();
		
		m_p1stCameraController = VS_NEW VS1stCameraController();
		m_pCameraActor->GetTypeNode()->AddController(m_p1stCameraController);

		VSVector3 CameraPos(00.0f,0.0f,-20.0f);
		VSVector3 CameraDir(0.0f,0.0f,1.0f);
		m_pCameraActor->GetTypeNode()->CreateFromLookDir(CameraPos,CameraDir);
		//m_pCameraActor->GetCameraNode()->CreateFromEuler(CameraPos,AngleToRadian(0),AngleToRadian(-90),0);
		m_pCameraActor->GetTypeNode()->SetPerspectiveFov(AngleToRadian(90.0f),(m_uiScreenWidth * 1.0f)/(m_uiScreenHeight),1.0f,1000.0f);
		VSViewPort ViewPort;
		ViewPort.XMax = 0.8f;
		ViewPort.YMax = 0.3f;
		ViewPort.bChangeAspect = true;
		m_pCameraActor->GetTypeNode()->AddViewPort(ViewPort);
		ViewPort.XMin = 0.2f;
		ViewPort.YMin = 0.5f;
		ViewPort.XMax = 1.0f;
		ViewPort.YMax = 1.0f;
		ViewPort.bChangeAspect = false;
		m_pCameraActor->GetTypeNode()->AddViewPort(ViewPort);
		

		

		//create texture and normal material
		VSString ShowName(_T("MaterialTexture"));

		//VSMaterialPtr pMaterial = VS_NEW VSMaterialDNSE(ShowName,NULL,NULL,NULL,NULL);
		//VSResourceManager::NewSaveMaterial(pMaterial,"TestMaterial");

		VSMaterialRPtr pMaterial = VSResourceManager::NewLoadASYNMaterial(_T("TestMaterial"),false);

		VSMaterialPtr pMaterial1 = VS_NEW VSMaterialOnlyTexture(ShowName,NULL);

		for (unsigned int i = 0 ; i < TEST_SKE_NUM * TEST_SKE_NUM ;i++)
		{
			m_pSkelMeshActor[i] = (VSSkelectonActor *)VSWorld::ms_pWorld->CreateActor(_T("test.SKMODEL"));
			//m_pSkelMeshClone[i]->SetIsDrawSkelecton(true);
			unsigned int m = i % TEST_SKE_NUM;
			unsigned int n = i / TEST_SKE_NUM;
			m_pSkelMeshActor[i]->SetLocalPos(VSVector3(n,0,m) * 30.0f);

			VSSkelectonMeshNode * pSkelMesh = m_pSkelMeshActor[i]->GetTypeNode();
			VSGeometryNodePtr pGeometryNode = pSkelMesh->GetGeometryNode(0);


			//pGeometryNode->GetGeometry(0)->AddMaterialInstance(VSMaterialR::Create(pMaterial));
			//pGeometryNode->GetGeometry(1)->AddMaterialInstance(VSMaterialR::Create(pMaterial));

			pGeometryNode->GetGeometry(0)->AddMaterialInstance(pMaterial);
			pGeometryNode->GetGeometry(1)->AddMaterialInstance(pMaterial);
			//pSkelMesh->SetIsDrawBoundVolume(true);
			pSkelMesh->SetLocalScale(VSVector3(0.3f,0.3f,0.3f));
			//pSkelMesh->SetIsDrawSkelecton(true);

			VSAnimSetPtr pAnimSet = VS_NEW VSAnimSet();
			pAnimSet->SetResourceName(_T("shou"));
			pAnimSet->LoadVSAction(_T("shou"));
 			pSkelMesh->SetAnimSet(pAnimSet);
 			pSkelMesh->PlayAnim(_T("shou"),1.0f,VSController::RT_WRAP);

			//pSkelMesh->SetLodType(VSMeshNode::LT_CLOD);
			//pSkelMesh->CreateClodMesh();

		
			
		}
		AddTexture();
		//create direction light

		m_pDirecttionLight = VS_NEW VSDirectionLightActor();
		VSDirectionLight* pDirectionLightNode = m_pDirecttionLight->GetTypeNode();
		pDirectionLightNode->m_Diffuse = VSVector3(1.0f,1.0f,1.0f);
		pDirectionLightNode->m_Specular = VSVector3(0.7f,0.7f,0.7f);

		m_pDirecttionLight1 = VS_NEW VSDirectionLightActor();
		VSDirectionLight* pDirectionLightNode1 = m_pDirecttionLight1->GetTypeNode();
		pDirectionLightNode1->m_Diffuse = VSVector3(1.0f,1.0f,1.0f);
		pDirectionLightNode1->m_Specular = VSVector3(0.7f,0.7f,0.7f);

		VSPointLightActorPtr pPointLight = VS_NEW VSPointLightActor();
		VSPointLight* pPointLightNode = pPointLight->GetTypeNode();
		pPointLightNode->SetLocalTranslate(VSVector3(0,200,0)) ;
		pPointLightNode->m_Diffuse = VSVector3(1.0f,1.0f,1.0f);
		pPointLightNode->m_Specular = VSVector3(0.3f,0.3f,0.3f);

		VSSceneMap * pSceneMap = VSWorld::ms_pWorld->GetScene(_T("Main"));
		
		pSceneMap->AddActor(m_pDirecttionLight);
		pSceneMap->AddActor(m_pDirecttionLight1);
		pSceneMap->AddActor(pPointLight);
		pSceneMap->AddActor(m_pCameraActor);
		
		VSArray<VSString> SceneMap;
		SceneMap.AddElement(_T("Main"));

		VSPESetGrayPtr pPEGraySet = VS_NEW VSPESetGray();
		m_pPEGraySet = VSPostEffectSetR::Create(pPEGraySet);
		VSWorld::ms_pWorld->AttachWindowViewFamilyToCamera(m_pCameraActor,VSWindowViewFamily::VT_WINDOW_NORMAL,
							_T("WindowUse"),SceneMap,-1,m_pPEGraySet,true,true);

		VSColorRGBA ClearColor(0.0f,0.0f,0.0f);

		VSRenderer::ms_pRenderer->SetClearColor(ClearColor);

		

		VSRasterizerDesc WireLessDesc;
		WireLessDesc.m_bWireEnable = true;
		m_pWireFrameStateEnable = VSResourceManager::CreateRasterizerState(WireLessDesc);


		//create terrain
		VSCLodTerrainActorPtr pQuadTerrainActor = VS_NEW VSCLodTerrainActor();
		VSCLodTerrainNode* pQuadTerrain = pQuadTerrainActor->GetTypeNode();
		pQuadTerrain->SetTerrainNodeType(VSCLodTerrainNode::TNT_QUAD);
		pQuadTerrain->SetScale(20);
		pQuadTerrain->SetLocalScale(VSVector3(5.0f,5.0f,5.0f));
		pQuadTerrain->SetLocalTranslate(VSVector3(0.0f,-300.0f,0.0f));
		//pQuadTerrain->CreateTerrain();
		pQuadTerrain->CreateTarrainFromHeightMap(_T("heightdate.raw"),6,0.2f);
		VSMaterial * pDefualtConlyColor = (VSMaterial *)VSMaterial::GetDefaultOnlyColor();
		pQuadTerrain->AddMaterialInstance(VSMaterialR::Create(pDefualtConlyColor));
		float yellow[4] = {1.0f,1.0f,0.0f,1.0f};
		pQuadTerrain->GetMaterialInstance(0)->SetPShaderValue(_T("EmissiveColor"),yellow,4);

		pSceneMap->AddActor(pQuadTerrainActor);


		VSCLodTerrainActorPtr pRoamTerrainActor = VS_NEW VSCLodTerrainActor();
		VSCLodTerrainNodePtr pRoamTerrain = pRoamTerrainActor->GetTypeNode();
		pRoamTerrain->SetTerrainNodeType(VSCLodTerrainNode::TNT_ROAM);
		pRoamTerrain->SetScale(200);
		pRoamTerrain->SetLocalScale(VSVector3(5.0f,5.0f,5.0f));
		pRoamTerrain->SetLocalTranslate(VSVector3(-1500.0f,-300.0f,0.0f));
		//pRoamTerrain->CreateTerrain();
		pRoamTerrain->CreateTarrainFromHeightMap(_T("heightdate.raw"),6,0.2f);
		pRoamTerrain->AddMaterialInstance(VSMaterialR::Create(pDefualtConlyColor));
		float green[4] = {0.0f,1.0f,0.0f,1.0f};
		pRoamTerrain->GetMaterialInstance(0)->SetPShaderValue(_T("EmissiveColor"),green,4);

		pSceneMap->AddActor(pRoamTerrainActor);


		VSDLodTerrainActorPtr pDLodTerrainActor = VS_NEW VSDLodTerrainActor();
		VSDLodTerrainNodePtr pDLodTerrain = pDLodTerrainActor->GetTypeNode();
		pDLodTerrain->SetLocalScale(VSVector3(5.0f,5.0f,5.0f));
		pDLodTerrain->SetLocalTranslate(VSVector3(0.0f,-300.0f,-1300.0f));
		pDLodTerrain->CreateTarrainFromHeightMap(_T("heightdate.raw"),6,0.2f);
		pDLodTerrain->AddMaterialInstance(VSMaterialR::Create(pDefualtConlyColor));
		float blue[4] = {0.0f,0.0f,1.0f,1.0f};
		pDLodTerrain->GetMaterialInstance(0)->SetPShaderValue(_T("EmissiveColor"),blue,4);
  		
		pSceneMap->AddActor(pDLodTerrainActor);


		//test code 
// 		VSBezierCurve3 Bc;
// 		VSVector3 P[4];
// 		P[0] = VSVector3(0.0f,0.0f,0.0f);
// 		P[1] = VSVector3(0.0f,100.0f,0.0f);
// 		P[2] = VSVector3(0.0f,300.0f,0.0f);
// 		P[3] = VSVector3(0.0f,600.0f,0.0f);
// 		Bc.Set(P,4);
// 		VSREAL t = Bc.GetTime(300.0f);
// 		VSCurve3SubdivisionByLength SB(10,0,1.0f,1000);
// 		SB.SetSubCurve(&Bc);
// 		SB.Subdivision();
// 		VSVector3 * mm = SB.GetPoints();
// 		VSSafeQueue<int> q;
// 		q.Enqueue(1);
// 		q.Enqueue(2);
// 		q.Enqueue(3);
// 		int k = 2;
// 		q.Erase(2);
// 		q.Dequeue(k);
// 		q.Dequeue(k);
// 		q.Dequeue(k);
// 		VSBitArray TempBitArray(12);
// 		TempBitArray.Set(2,1);
// 		bool t = TempBitArray[3];
// 		bool t1 = TempBitArray[2];

// 		VSArrayOrder<int> l;
// 		l.AddElement(3);
// 		l.AddElement(4);
// 		l.AddElement(5);
// 		l.AddElement(2);
// 
// 		l.AddElement(19);
// 		l.AddElement(6);
// 		l.AddElement(1);
// 		l.AddElement(20);
// 		l.AddElement(7);
// 		l[2] = 32;
// 		l.ReSort(2);

		VSREAL Map[10][10]= {
			{1.0f,1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f},
			{0.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,0.0f,0.0f},
			{0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,0.0f},
			{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,3.0f},
			{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
			{0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f},
			{0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f},
			{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,1.0f},
			{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f},
			{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f},
							};
		VSGraph * pGraph = CreateFromAdjacencyMatrix<VSGraph,10>(Map);

		VSList<VSGraphNode *> & NodeList = pGraph->GetAllNode();
		VSList<VSGraphNode *>::VSListIterator It = NodeList.Begin();
		VSGraphNode * pGraphNode[10] = {NULL};
		unsigned int uiIndexNode = 0;
		for (;It.IsValid() ; ++It)
		{
			pGraphNode[uiIndexNode] = (*It);
			uiIndexNode++;
		}
		VSGraph_SearchDijkstra Search(pGraph,pGraphNode[0],pGraphNode[9]);
		VSREAL fCost = Search.GetCostToTarget();
		VSArray<VSGraphNode *> Temp;
		Temp = Search.GetPathToTarget();
		VSGraph_SearchAStar<VSHeuristic> Search1(pGraph,pGraphNode[0],pGraphNode[9]);
		VSREAL fCost1 = Search1.GetCostToTarget();
		VSArray<VSGraphNode *> Temp1;
		Temp1 = Search1.GetPathToTarget();
		VSMAC_DELETE(pGraph);

		//test new stream

		VSTestSaveLoadPtr pTestSaveLoad = VS_NEW VSTestSaveLoad();
		pTestSaveLoad->m_Int = 2.0f;
		pTestSaveLoad->m_ArrayInt.SetBufferNum(4);
		for (unsigned int i = 0 ; i < pTestSaveLoad->m_ArrayInt.GetNum() ; i++)
		{
			pTestSaveLoad->m_ArrayInt[i] = i;
		}
		pTestSaveLoad->m_FloatDataNum = 7;
		pTestSaveLoad->m_FloatData = VS_NEW float[pTestSaveLoad->m_FloatDataNum];

		for (unsigned int i = 0 ; i < pTestSaveLoad->m_FloatDataNum ; i++)
		{
			pTestSaveLoad->m_FloatData[i] = i * 0.3f;
		}
		pTestSaveLoad->m_Name = _T("Fuck");
		pTestSaveLoad->m_TestStruct.m_sFloat = 123.01f;
		pTestSaveLoad->m_TestStruct.m_sInt = 34;

		pTestSaveLoad->m_pTestObject1 = VS_NEW VSTestObject();
		pTestSaveLoad->m_pTestObject1->m_TestFloat = 4.1f;
		pTestSaveLoad->m_pTestObject1->m_TestInt = 991;

		pTestSaveLoad->m_pTestObject2 = VS_NEW VSTestObject();
		pTestSaveLoad->m_pTestObject2->m_TestFloat = 4.2f;
		pTestSaveLoad->m_pTestObject2->m_TestInt = 992;

		pTestSaveLoad->m_TestObject3.m_TestFloat = 4.3f;
		pTestSaveLoad->m_TestObject3.m_TestInt = 993;

		pTestSaveLoad->m_TestString = "Test";
		VSTestSaveLoadPtr pCopyTest = (VSTestSaveLoad *)VSObject::CloneCreateObject(pTestSaveLoad);
 		VSStream TestStream;

// 		TestStream.SetStreamFlag(VSStream::AT_REGISTER);
// 		TestStream.ArchiveAll(pTestSaveLoad);
// 
// 		TestStream.NewSave("test_stream");
// 		TestStream.NewLoad("test_stream");
// 		pTestSaveLoad = (VSTestSaveLoad *)TestStream.GetObjectByRtti(VSTestSaveLoad::ms_Type);
// 		VSTestSaveLoadChild::Test();

// 		VSSamplerDesc SamplerDesc;
// 		SamplerDesc.m_uiMag = VSSamplerDesc::FM_LINE;
// 		SamplerDesc.m_uiMin = VSSamplerDesc::FM_LINE;
// 		SamplerDesc.m_uiMip = VSSamplerDesc::FM_LINE;
// 		VSSamplerStatePtr pSamplerState = VSResourceManager::CreateSamplerState(SamplerDesc);
// 		VSTexAllStateRPtr pTexDiffuse = VSResourceManager::LoadASYN2DTextureCompress(_T("shou_d.tga"),false,pSamplerState,2,false,true);
// 		
// 		TestStream.SetStreamFlag(VSStream::AT_REGISTER);
// 		TestStream.ArchiveAll(pTexDiffuse->GetResource());
// 
// 		TestStream.NewSave("test_stream");

// 		TestStream.NewLoad("test_stream");
// 		VSTexAllStatePtr pTestTexture = (VSTexAllState *)TestStream.GetObjectByRtti(VSTexAllState::ms_Type);
// 		VSTexAllStateR * pTexAllState = NULL;
// 		
// 		pTexAllState = VS_NEW VSTexAllStateR();
// 		pTexAllState->SetResourceName(_T("shou_d.tga"));
// 		pTexAllState->SetNewResource(pTestTexture);
// 		pTexAllState->Loaded();
// 		VSResourceManager::GetASYNTextureSet().AddResource(_T("shou_d.tga"),pTexAllState);
// #define  TEXTURE_SIZE 16
// #define  DETA 255.0f/(TEXTURE_SIZE - 1)
// 		for (unsigned k = 0 ; k < TEXTURE_SIZE ; k++)
// 		{
// 			VS2DTexturePtr pTexture = VS_NEW VS2DTexture(TEXTURE_SIZE,TEXTURE_SIZE,VSRenderer::SFT_A8R8G8B8,1,true);
// 			pTexture->CreateRAMDate();
// 			for (unsigned int cy = 0; cy < TEXTURE_SIZE; cy++) 
// 			{
// 				for (unsigned int cx = 0; cx < TEXTURE_SIZE; cx++) 
// 				{
// 
// 					unsigned uiIndex = cy * TEXTURE_SIZE + cx;
// 					unsigned char *pBuffer = pTexture->GetBuffer(0,uiIndex);
// 	
// 
// 					pBuffer[0] = (unsigned char)(DETA * k);
// 					pBuffer[1] = (unsigned char)(DETA * cy);
// 					pBuffer[2] = (unsigned char)(DETA * cx);
// 					pBuffer[3] = 255;
// 	
// 				} // for
// 			} // for
// 			VSString FileName,FileID;
// 			FileID.Format("%d.bmp",k);
// 			FileName = "SaveTexture" + FileID;
// 			pTexture->SaveToFile(FileName.GetBuffer());
// 		}
		return true;
	}
	bool VSSample::OnTerminal()
	{
		if (!VSApplication::OnTerminal())
		{
			return false;
		}
		for (unsigned int i = 0 ; i < TEST_SKE_NUM * TEST_SKE_NUM ;i++)
		{
			m_pSkelMeshActor[i] = NULL;
		}

		m_pCameraActor = NULL;

		m_p1stCameraController = NULL;
		m_pWireFrameStateEnable = NULL;
		m_pDirecttionLight = NULL;
		m_pDirecttionLight1 = NULL;
		m_pPEGraySet = NULL;
		return true;
	}
	bool VSSample::PostUpdate()
	{
		if (!VSApplication::PostUpdate())
		{
			return false;
		}
		double dAppTime = VSTimer::ms_pTimer->GetGamePlayTime();
		static double LastTime = dAppTime;
		double DetTime = dAppTime - LastTime;
		LastTime = dAppTime;
		m_p1stCameraController->m_RotXDelta = 0.0f;
		m_p1stCameraController->m_RotYDelta = 0.0f;
		m_fAngle += 0.1f * DetTime;
// 		ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(KKK,double,DetTime,DetTime,{VSOutPutDebugString("%f\n",DetTime);})
// 			{VSOutPutDebugString("%f\n",DetTime);}
// 		ENQUEUE_UNIQUE_RENDER_COMMAND_END
		if (m_fAngle > 360)
		{
			m_fAngle = 0.0f;
		}
		static VSMatrix3X3 Rotator;
		Rotator.CreateRotXYZ(0,AngleToRadian(m_fAngle),0);
		m_pDirecttionLight->SetLocalRotate(Rotator);

		Rotator.CreateRotXYZ(AngleToRadian(m_fAngle),0,0);
		m_pDirecttionLight1->SetLocalRotate(Rotator);
		return true;
	}
	void VSSample::AddTexture()
	{
		VSSamplerDesc SamplerDesc;
		SamplerDesc.m_uiMag = VSSamplerDesc::FM_LINE;
		SamplerDesc.m_uiMin = VSSamplerDesc::FM_LINE;
		SamplerDesc.m_uiMip = VSSamplerDesc::FM_LINE;
		VSSamplerStatePtr pSamplerState = VSResourceManager::CreateSamplerState(SamplerDesc);
		//create texture
		
		VSTexAllStateRPtr pTexDiffuse = VSResourceManager::NewLoadASYNTexture(_T("TestDiffuse"),false);
		//VSTexAllStateRPtr pTexDiffuse = VSResourceManager::LoadASYN2DTextureCompress(_T("shou_d.tga"),false,pSamplerState,2,false,true);
		//VSResourceManager::NewSaveTexture(pTexDiffuse->GetResource(),_T("TestDiffuse"));

		VSTexAllStateRPtr pTexNormal = VSResourceManager::NewLoadASYNTexture(_T("TestNormal"),false);
		//VSTexAllStateRPtr pTexNormal= VSResourceManager::LoadASYN2DTextureCompress(_T("shou_n.tga"),false,pSamplerState,2,true);
		//VSResourceManager::NewSaveTexture(pTexNormal->GetResource(),_T("TestNormal"));

		VSTexAllStateRPtr pTexSpecular = VSResourceManager::NewLoadASYNTexture(_T("TestSpecular"),false);
		//VSTexAllStateRPtr pTexSpecular = VSResourceManager::LoadASYN2DTexture(_T("shou_s.tga"),false,pSamplerState);
		//VSResourceManager::NewSaveTexture(pTexSpecular->GetResource(),_T("TestSpecular"));

		VSTexAllStateRPtr pTexEmissive = VSResourceManager::NewLoadASYNTexture(_T("TestEmissive"),false);
		//VSTexAllStateRPtr pTexEmissive = VSResourceManager::LoadASYN2DTexture(_T("shou_e.tga"),false,pSamplerState);
		//VSResourceManager::NewSaveTexture(pTexEmissive->GetResource(),_T("TestEmissive"));
	

		VSTexAllStateRPtr pTexWDiffuse = VSResourceManager::LoadASYN2DTexture(_T("shou_w_d.tga"),false,pSamplerState);

		VSTexAllStateRPtr pTexWNormal= VSResourceManager::LoadASYN2DTexture(_T("shou_w_n.tga"),false,pSamplerState);

		VSTexAllStateRPtr pTexWSpecular = VSResourceManager::LoadASYN2DTexture(_T("shou_w_s.tga"),false,pSamplerState);

		VSTexAllStateRPtr pTexWEmissive = VSResourceManager::LoadASYN2DTexture(_T("shou_w_e.tga"),false,pSamplerState);


		for (unsigned int i = 0 ; i < TEST_SKE_NUM * TEST_SKE_NUM ; i++)
		{
			if (m_pSkelMeshActor[i])
			{
				VSGeometryNode* pGeometryNode = m_pSkelMeshActor[i]->GetTypeNode()->GetGeometryNode(0);

				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("DiffuseTexture"),pTexDiffuse);
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("NormalTexture"),pTexNormal);
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("SpecularTexture"),pTexSpecular);
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("EmissiveTexture"),pTexEmissive);
				VSREAL SpePow = 50.0f;
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderValue(_T("SpecularPow"),&SpePow,1);

				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("DiffuseTexture"),pTexWDiffuse);
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("NormalTexture"),pTexWNormal);
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("SpecularTexture"),pTexWSpecular);
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("EmissiveTexture"),pTexWEmissive);
				SpePow = 5.0f;
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderValue(_T("SpecularPow"),&SpePow,1);
			}
		}
	}
	void VSSample::AddASYNTexture()
	{
		VSSamplerDesc SamplerDesc;
		SamplerDesc.m_uiMag = VSSamplerDesc::FM_LINE;
		SamplerDesc.m_uiMin = VSSamplerDesc::FM_LINE;
		SamplerDesc.m_uiMip = VSSamplerDesc::FM_LINE;
		VSSamplerStatePtr pSamplerState = VSResourceManager::CreateSamplerState(SamplerDesc);
		//create texture
		VSTexAllStateRPtr pTexDiffuse = VSResourceManager::LoadASYN2DTextureCompress(_T("shou_d.tga"),true,pSamplerState,2,false,true);

		VSTexAllStateRPtr pTexNormal= VSResourceManager::LoadASYN2DTextureCompress(_T("shou_n.tga"),true,pSamplerState,2,true);

		VSTexAllStateRPtr pTexSpecular = VSResourceManager::LoadASYN2DTexture(_T("shou_s.tga"),true,pSamplerState);

		VSTexAllStateRPtr pTexEmissive = VSResourceManager::LoadASYN2DTexture(_T("shou_e.tga"),true,pSamplerState);



		VSTexAllStateRPtr pTexWDiffuse = VSResourceManager::LoadASYN2DTexture(_T("shou_w_d.tga"),true,pSamplerState);

		VSTexAllStateRPtr pTexWNormal= VSResourceManager::LoadASYN2DTexture(_T("shou_w_n.tga"),true,pSamplerState);

		VSTexAllStateRPtr pTexWSpecular = VSResourceManager::LoadASYN2DTexture(_T("shou_w_s.tga"),true,pSamplerState);

		VSTexAllStateRPtr pTexWEmissive = VSResourceManager::LoadASYN2DTexture(_T("shou_w_e.tga"),true,pSamplerState);


		for (unsigned int i = 0 ; i < TEST_SKE_NUM * TEST_SKE_NUM ; i++)
		{
			if (m_pSkelMeshActor[i])
			{
				VSGeometryNode* pGeometryNode = m_pSkelMeshActor[i]->GetTypeNode()->GetGeometryNode(0);

				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("DiffuseTexture"),pTexDiffuse);
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("NormalTexture"),pTexNormal);
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("SpecularTexture"),pTexSpecular);
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderTexture(_T("EmissiveTexture"),pTexEmissive);
				VSREAL SpePow = 50.0f;
				pGeometryNode->GetGeometry(0)->GetMaterialInstance(0)->SetPShaderValue(_T("SpecularPow"),&SpePow,1);

				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("DiffuseTexture"),pTexWDiffuse);
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("NormalTexture"),pTexWNormal);
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("SpecularTexture"),pTexWSpecular);
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderTexture(_T("EmissiveTexture"),pTexWEmissive);
				SpePow = 5.0f;
				pGeometryNode->GetGeometry(1)->GetMaterialInstance(0)->SetPShaderValue(_T("SpecularPow"),&SpePow,1);
			}
		}

	}
	bool VSSample::OnDraw()
	{
		if (!VSApplication::OnDraw())
		{
			return false;
		}
		VSRenderer::ms_pRenderer->DrawText(NULL,0,100,VSColorRGBA(1.0f,1.0f,0.0f),_T("Mouse X,Y:%d,%d"),m_iMouseX,m_iMouseY);
		return true;
	}
	void VSSample::OnMove(int xPos,int yPos)
	{
		VSApplication::OnMove(xPos,yPos);
	}
	void VSSample::OnReSize(int iWidth,int iHeight)
	{
		VSApplication::OnReSize(iWidth,iHeight);
	}
	void VSSample::OnKeyDown(unsigned int uiKey)
	{
		VSApplication::OnKeyDown(uiKey);
		if (uiKey == VSEngineInput::BK_UP)
		{
			m_p1stCameraController->m_MoveZDelta = 0.05f;
		}
		else if (uiKey == VSEngineInput::BK_DOWN)
		{
			m_p1stCameraController->m_MoveZDelta = -0.05f;
		}
		else if (uiKey == VSEngineInput::BK_LEFT)
		{
			m_p1stCameraController->m_MoveXDelta = -0.05f;
		}	
		else if (uiKey == VSEngineInput::BK_RIGHT)
		{
			m_p1stCameraController->m_MoveXDelta = 0.05f;
		}
		else if (uiKey == VSEngineInput::BK_W)
		{
			m_bWireSwith = !m_bWireSwith;

			VSViewFamily * pViewFamily = VSSceneManager::ms_pSceneManager->GetViewFamily(_T("WindowUse"));
			if (m_bWireSwith)
			{
				VSRenderState RenderState;
				RenderState.SetRasterizerState(m_pWireFrameStateEnable);
				
				pViewFamily->SetUseState(RenderState,VSRenderState::IF_WIRE_ENABLE);
			}
			else
			{
				pViewFamily->ClearUseState();
			}

			
		}
		else if (uiKey == VSEngineInput::BK_P)
		{
			VSPostEffectFunction * pPEFuntion = m_pPEGraySet->GetResource()->GetPEFunction(_T("Gray"));
			pPEFuntion->m_bEnable = !pPEFuntion->m_bEnable;


		}
		else if (uiKey == VSEngineInput::BK_MINUS)
		{
			m_fPencent -= 0.1f;
			if (m_fPencent < 0.0f)
			{
				m_fPencent = 0.0f;
			}
			//m_pSkelMesh->SetLodDesirePercent(m_fPencent);
		}
		else if (uiKey == VSEngineInput::BK_EQUALS)
		{
			m_fPencent += 0.1f;
			if (m_fPencent > 1.0f)
			{
				m_fPencent = 1.0f;
			}
			//m_pSkelMesh->SetLodDesirePercent(m_fPencent);
		}
		else if (uiKey == VSEngineInput::BK_L)
		{
			AddASYNTexture();
		}
	}
	void VSSample::OnKeyUp(unsigned int uiKey)
	{
		VSApplication::OnKeyUp(uiKey);
		if (uiKey == VSEngineInput::BK_UP)
		{
			m_p1stCameraController->m_MoveZDelta = 0.0f;
		}
		else if (uiKey == VSEngineInput::BK_DOWN)
		{
			m_p1stCameraController->m_MoveZDelta = 0.0f;
		}
		else if (uiKey == VSEngineInput::BK_LEFT)
		{
			m_p1stCameraController->m_MoveXDelta = 0.0f;
		}	
		else if (uiKey == VSEngineInput::BK_RIGHT)
		{
			m_p1stCameraController->m_MoveXDelta = 0.0f;
		}
		else if (uiKey == VSEngineInput::BK_1)
		{
			ChangeScreenSize(640,480,true);
			m_pCameraActor->GetTypeNode()->SetAspect((m_uiScreenWidth * 1.0f) / m_uiScreenHeight);
		}
		else if (uiKey == VSEngineInput::BK_2)
		{
			ChangeScreenSize(960,600,true,true);
			m_pCameraActor->GetTypeNode()->SetAspect((m_uiScreenWidth * 1.0f) / m_uiScreenHeight);

		}
		else if (uiKey == VSEngineInput::BK_3)
		{
			ChangeScreenSize(1024,768,true);
			m_pCameraActor->GetTypeNode()->SetAspect((m_uiScreenWidth * 1.0f) / m_uiScreenHeight);
		}
		else if (uiKey == VSEngineInput::BK_4)
		{
			ChangeScreenSize(1440,900,true);
			m_pCameraActor->GetTypeNode()->SetAspect((m_uiScreenWidth * 1.0f) / m_uiScreenHeight);
		}
		else if (uiKey == VSEngineInput::BK_5)
		{
			ChangeScreenSize(1280,800,false);
			m_pCameraActor->GetTypeNode()->SetAspect((m_uiScreenWidth * 1.0f) / m_uiScreenHeight);
		}
		else if (uiKey == VSEngineInput::BK_6)
		{
			ChangeScreenSize(1024,768,true,true);
			m_pCameraActor->GetTypeNode()->SetAspect((m_uiScreenWidth * 1.0f) / m_uiScreenHeight);
		}
		else if (uiKey == VSEngineInput::BK_7)
		{
			ChangeScreenSize(1024,768,false);
			m_pCameraActor->GetTypeNode()->SetAspect((m_uiScreenWidth * 1.0f) / m_uiScreenHeight);
		}
	}
	void VSSample::OnLButtonDown(int xPos,int yPos)
	{
		VSApplication::OnLButtonDown(xPos,yPos);
		m_bLMouseDowning = true;
		m_iLastMouseX = xPos;
		m_iLastMouseY = yPos;
		//ShowCursor(false);
	}
	void VSSample::OnLButtonUp(int xPos,int yPos)
	{
		VSApplication::OnLButtonUp(xPos,yPos);
		m_bLMouseDowning = false;
		//ShowCursor(true);
	}
	void VSSample::OnRButtonDown(int xPos,int yPos)
	{
		VSApplication::OnRButtonDown(xPos,yPos);
		
	}
	void VSSample::OnRButtonUp(int xPos,int yPos)
	{
		VSApplication::OnRButtonUp(xPos,yPos);
		
	}
	void VSSample::OnMButtonDown(int xPos,int yPos)
	{
		VSApplication::OnMButtonDown(xPos,yPos);
	}
	void VSSample::OnMButtonUp(int xPos,int yPos)
	{
		VSApplication::OnMButtonUp(xPos,yPos);
	}
	void VSSample::OnMouseMove(int xPos,int yPos)
	{
		VSApplication::OnMouseMove(xPos,yPos);
		m_iMouseX = xPos;
		m_iMouseY = yPos;
		if (m_bLMouseDowning)
		{
			//SetCapture(m_MainHwnd);
			int iDetX = m_iMouseX - m_iLastMouseX;
			int iDetY = m_iMouseY - m_iLastMouseY;
			m_p1stCameraController->m_RotXDelta = ((iDetY) * 1.0f)/1500.0f;
			m_p1stCameraController->m_RotYDelta = ((iDetX) * 1.0f)/1500.0f;
			m_iLastMouseX = m_iMouseX;
			m_iLastMouseY = m_iMouseY;
		}
		
	}
	void VSSample::OnMouseWheel(int xPos,int yPos,int zDet)
	{
		VSApplication::OnMouseWheel(xPos,yPos,zDet);
	}
}
#endif